<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      position: absolute;
      left: 50%;
      top: 50%;
      border: 1px solid black;
      transform: translate(-50%, -50%);
      display: flex;
      flex-direction: column;
      padding: 15px;
    }

    input,
    button {
      margin: 10px;
    }
  </style>
</head>

<body>
  <div>
    <input type="text" placeholder="请输入账号">
    <input type="password" placeholder="请输入密码">
    <button>登录</button>
    <button>注册</button>
  </div>
</body>
<script>
  var input = document.getElementsByTagName('input')
  var button = document.getElementsByTagName('button')

  var weather = 'https://api.muxiaoguo.cn/api/tianqi?city=成都&type=1&api_key=09d664ccb044efc8'
  fetch(weather)
    .then(response => response.json())
    .then(data => {
      console.log(data)
    })
    .catch(error => {
      alert('登录失败！')
      console.log(error)
    })


  var url = 'http://127.0.0.1:1314'
  button[0].onclick = function () {
    var data = {
      username: input[0].value,
      password: input[1].value
    }

    fetch(url + '/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    })
      .then(response => response.json())
      .then(data => {
        console.log(data)
        if (data.code == 200) {
          alert('登录成功！')
        }
      })
      .catch(error => {
        alert('登录失败！')
        console.log(error)
      })
  }

  button[1].onclick = function () {
    var data = {
      username: input[0].value,
      password: input[1].value
    }

    fetch(url + '/register', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    })
      .then(response => response.json())
      .then(data => {
        if (data.code == 200) {
          alert('注册成功！')
        }
        console.log(data)
      })
      .catch(error => {
        alert('注册失败！')
        console.log(error)
      })
  }
</script>

</html>